<?php

/**
 * This is the model class for table "cameras".
 *
 * The followings are the available columns in table 'cameras':
 * @property integer $id
 * @property integer $sequence
 * @property string $location
 */
class Camera extends ActiveRecord {

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Camera the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return 'cameras';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		return array(
			array(
				array(
					'sequence',
				),
				'numerical',
				'integerOnly'=>true,
			),
			array(
				array(
					'location',
				),
				'length',
				'max'=>255,
			),
			array(
				array(
					'id',
					'sequence',
					'location',
				),
				'safe',
				'on'=>'search',
			),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array(
			'id'=>'ID',
			'sequence'=>'Sequence',
			'location'=>'Location',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models
	 *   based on the search/filter conditions.
	 */
	public function search() {
		$criteria = new CDbCriteria;

		$criteria->compare('id', $this->id);
		$criteria->compare('sequence', $this->sequence);
		$criteria->compare('location', $this->location, true);

		return new CActiveDataProvider($this, array('criteria'=>$criteria));
	}
	
	public function getImageExists() {
		return file_exists($this->imageFile);
	}
	
	public function getImageFile() {
		$folder = dirname(Yii::app()->request->scriptFile);
		$path = "{$folder}/files/camera/{$this->sequence}.jpg";
		return $path;
	}
	
	public function getDefaultImageFile() {
		$folder = dirname(Yii::app()->request->scriptFile);
		$path = "{$folder}/files/camera/no-video.jpg";
		return $path;
	}
	
	public function getImageUrl() {
		$request = Yii::app()->request;
		$host = $request->hostInfo;
		$baseUrl = $request->baseUrl;
		return "{$host}{$baseUrl}/files/camera/{$this->sequence}.png";
	}
	
	public function getModifiedTime() {
		$format = Yii::app()->dateFormatter;
		$time = filemtime($this->imageFile);
		return $format->format("d MMMM y HH:mm:s", $time);
	}

}